import streamlit as st
import streamlit_antd_components as sac
from .calculator import calculator_management
from .user_management import user_management_page
from .calculator_logs import calc_audit_logs_page
from .statusadmin import status_management
from .menu_manager import MenuManager
from .system_config import system_config_page

def admin_page():
    """管理页面"""
    menu_manager = MenuManager.get_instance(reset=True)

    # 创建功能管理分组

    # 检查是否正在编辑特定计算器
    is_editing_calculator = st.session_state.get("selected_calc") not in [None, "最近的计算器"]
    if not is_editing_calculator:
        menu_manager.register(
            "admin_group",
            lambda: None,  # 分组菜单不需要处理函数
            label="功能管理",
            type="group",
            children=[]  # 先创建空的子菜单列表
        )

    # 根据角色注册子菜单项
    if "sysadmin" in st.session_state.roles:
        menu_manager.register(
            "user", user_management_page,
            label="用户管理", icon="people-fill",
            parent="admin_group"
        )
        menu_manager.register(
            "system_config", system_config_page,
            label="基础配置", icon="gear-fill",
            parent="admin_group"
        )

    if "calcadmin" in st.session_state.roles:
        menu_manager.register(
            "system", calc_audit_logs_page,
            label="计算日志", icon="file-earmark-diff-fill",
            parent="admin_group"
        )
        calculator_management()

    if "statusadmin" in st.session_state.roles:
        menu_manager.register(
            "statusadmin", status_management,
            label="计算器状态管理", icon="check-circle-fill",
            parent="admin_group"
        )


    menu_manager.show_menu(index=1)